/*
 * berton
 */
package com.pitop.t.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.TuserException;
import com.pitop.t.dao.TuserDAO;
import com.pitop.t.entity.User;
import com.pitop.t.entity.UserState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("tuserSV")
@Slf4j
public class TuserSVImpl extends BaseSVImpl<User, Long> implements TuserSV {

    @Autowired
    private TuserDAO tuserDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return tuserDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(User entity) throws BaseException {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象Tuser
     * @param userId 
     * @return User
     */
    @Override
    public User load(java.lang.Integer userId) {
           if(userId==null){
                 throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("userId",userId);
            return tuserDAO.load(param);
    }
    /**
     * 加载一个对象Tuser 通过userId
     * @param userId 
     * @return User
     */
    @Override
    public User loadByUserId(java.lang.Integer userId) {
            if(userId==null){
                 throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return tuserDAO.loadByUserId(userId);
    }


    /**
    * 根据主键userId,oldStates 共同更新 User 的状态到newState状态
    *
    * @param userId 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByUserId(java.lang.Integer userId, UserState newState, UserState... oldStates){
        if(userId==null){
            throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        tuserDAO.updateStateByUserId(userId,new Date(),newState,oldStates);
    }

    /**
    * 根据主键userId 更新 User 的状态到另一个状态
    *
    * @param userId 
    * @param state 状态
    */
    @Override
    public void updateByUserId(java.lang.Integer userId,UserState state){
            if(userId==null){
               throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          tuserDAO.updateByUserId(userId,state,new Date());
    }


    /**
     * 删除对象Tuser
     * @param userId 
     * @return User
     */
    @Override
    public void delete(java.lang.Integer userId) {
            if(userId==null){
                throw new TuserException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("userId",userId);
            tuserDAO.delete(param);
    }



    /**
    * 查询Tuser分页
    *
    * @param tuser  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<User>
    */
    @Override
    public List<User> list(User tuser, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (tuser != null) {
               map = JSON.parseObject(JSON.toJSONString(tuser, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return tuserDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(User tuser){
        Map<String, Object> map = null;
        if (tuser != null) {
           map = JSON.parseObject(JSON.toJSONString(tuser, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return tuserDAO.count(map);
     }

    /**
     * 查询Tuser分页
     *
     * @param userId  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<User>
     */
    @Override
    public List<User> list(java.lang.Integer userId, int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("userId",userId);
            return tuserDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Integer userId) {
            Map<String,Object> param=new HashMap<>();
            param.put("userId",userId);
            return tuserDAO.count(param);
    }

    @Override
    public User findUserByUserName(String userName) {
        Map<String,Object> param=new HashMap<>();
        param.put("userName",userName);
        return tuserDAO.load(param);
    }

}
